<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/edit.css">
    <!-- 引入 editor.md 的依赖 -->
    <link rel="stylesheet" href="editor.md/css/editormd.min.css" />
    <script src="js/jquery.min.js"></script>
    <script src="editor.md/lib/marked.min.js"></script>
    <script src="editor.md/lib/prettify.min.js"></script>
    <script src="editor.md/editormd.js"></script>
</head>
<body>
    <div class="nav">
        <img src="image/头像.jpg" alt="头像">
        <span class="title1">我的博客系统</span>
        <a href="home.html">主页</a>
        <a href="personlist.html">我的</a>
        <a href="edit.html">创作</a>
        <a href="logout">注销</a>
    </div>
    <div class="leader">
        <!-- <form action="edit" method="post" style="height: 100%;"> -->
            <div class="empOne">
                <input type="text" class="title" value="在这里写下文章标题" onblur="if(this.value == '')this.value='在这里写下文章标题';" onclick="if(this.value == '在这里写下文章标题')this.value='';" name="title">
                <input type="submit" value="发布文章" class="publish">
            </div>
            <div id="editor">
                <textarea name="content" style="display: none;" class="content"></textarea>
            </div>
        <!-- </form> -->
    </div>

    <script>
        // 初始化编辑器
        var editor = editormd("editor", {
            // 这里的尺寸必须在这里设置. 设置样式会被 editormd 自动覆盖掉.
            width: "100%",
            // 高度 100% 意思是和父元素一样高. 要在父元素的基础上去掉标题编辑区的高度
            height: "calc(100% - 75px)",
            // 编辑器中的初始内容
            markdown: "# 在这里写下一篇博客",
            // 指定 editor.md 依赖的插件路径
            path: "editor.md/lib/",
            // 放到 textarea中
            saveHTMLToTextArea: true
        });
        
        let submit = document.querySelector(".publish");
        submit.onclick = function() {
            let title = document.querySelector('.title');
            let content = document.querySelector('.content');
            $.ajax({
                url: "edit",
                method: "POST",
                data: JSON.stringify({title: title.value, content: content.value}),
                contentType: "application/json;charset=utf-8",
                success:function(data,status){
                    if(data.message == null){
                        location.assign("home.html");
                    }else{
                        alert(data.message);
                    }
                }
            })
        }
    </script>
</body>
</html>